package com.atguigu.tingshu.payment.api;

import com.atguigu.tingshu.common.login.UserLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.payment.service.WxPayService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Tag(name = "微信支付接口")
@RestController
@RequestMapping("api/payment")
@Slf4j
public class WxPayApiController {

    @Autowired
    private WxPayService wxPayService;

    @PostMapping("/wxPay/createJsapi/{paymentType}/{orderNo}")
    @UserLogin
    public Result<Map<String,Object>> createJsapi(@PathVariable String paymentType, @PathVariable String orderNo){
        Long userId = AuthContextHolder.getUserId();
        Map<String,Object> map = wxPayService.createJsapi(userId,paymentType,orderNo);
        return Result.ok(map);
    }

    /**
     * 小程序轮询查询支付结果-根据商户订单编号查询交易状态
     *
     * @param orderNo
     * @return
     */
    @GetMapping("/wxPay/queryPayStatus/{orderNo}")
    public Result<Boolean> queryPayStatus(@PathVariable String orderNo){
        Boolean isPay = wxPayService.queryPayStatus(orderNo);
        return Result.ok(isPay);
    }

    /**
     * 提供给微信支付进行异步回调接口
     *
     * @param request
     * @return
     */
    @PostMapping("/wxPay/notify")
    public Map<String, String> paySuccessNotify(HttpServletRequest request) {
        Map<String, String> mapResult = wxPayService.paySuccessNotify(request);
        return mapResult;
    }

    @GetMapping("/test")
    public String hello(){
        return "hello";
    }


}
